﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PreContractWeb.Administration
{
    public partial class EditUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Header.Controls.Add(new LiteralControl(@"<base target=""_self"" />"));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);


            bool isRoleAdmin = this.CheckUserPermission()[1];

            if (!isRoleAdmin)
            {
                this.divError.InnerHtml = AlertMessage.NoRoleAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            }
            else
            {
                this.GetBD();
                this.GetCAT();
                this.GetProvince();

                if (!Page.IsPostBack)
                {
                    if (Request["id"] != null)
                    {
                        this.SaveButton.Text = "保存(Save)";
                        this.CNTitleLabel.Text = "编辑";
                        this.ENTitleLabel.Text = "Edit";

                        this.GetUserInfo();
                    }
                }

                Response.Cache.SetCacheability(HttpCacheability.NoCache);
            }

        }

        protected bool[] CheckUserPermission()
        {
            BEGDC.PreContract.BLL.IUserRole iuser = (BEGDC.PreContract.BLL.IUserRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_UserRole_Service];

            DataTable dt = iuser.GetRoleListByEmployeeId(((BEGDC.PreContract.BLL.Authentication.PCIdentity)Page.User.Identity).EmployeeId);

            bool IsAdmin = false;
            bool IsRoleAdmin = false;
            string role = string.Empty;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                role = dt.Rows[i][0].ToString();
                if (role.Equals("1"))
                    IsAdmin = true;
                else if (role.Equals("4"))
                    IsRoleAdmin = true;
            }

            return new bool[] { IsAdmin, IsRoleAdmin };

        }

        protected void GetUserInfo()
        {
            try
            {
                int userId = Convert.ToInt32(Request["id"].ToString());

                BEGDC.PreContract.BLL.IEmployee iEmployee = (BEGDC.PreContract.BLL.IEmployee)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Employee_Service];
                BEGDC.PreContract.DAL.Employee employee = iEmployee.GetEmployee(userId);

                int asbOrLucent = employee.ASBOrLucent;

                if (employee.Active)
                    switch (asbOrLucent)
                    {
                        case 0: this.rbtASB.Checked = true; break;
                        case 1: this.rbtLucent.Checked = true; break;
                        case 2: this.rbtAll.Checked = true; break;
                    }
                else
                    this.rbtAll.Checked = true;

                this.ActiveCheckBox.Checked = employee.Active;
                this.DownloadChenckBox.Checked = employee.DownloadAllowed;

                this.LabelCName.Text = employee.CName;
                this.LabelEName.Text = employee.EName;
                this.LabelEmail.Text = employee.Email;
                this.LabelCILAccount.Text = employee.ADAccount;
                this.LabelBD.Text = employee.BD;
                this.LabelTitle.Text = employee.Title;
                this.LabelCompany.Text = employee.Company;
                this.LabelPhone.Text = employee.Phone;
                this.CommentsCheckBox.Checked = employee.CommentAllowed;
                this.LabelBadge.Text = employee.Badge;
                this.GetRole();
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }


        #region CAT
        protected void GetCAT()
        {
            try
            {
                int id = Convert.ToInt32(Request["id"].ToString());
                BEGDC.PreContract.BLL.ICAT iCAT = (BEGDC.PreContract.BLL.ICAT)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CAT_Service];

                DataTable dt = iCAT.GetCAT_DataTable("1=1 ORDER BY Active DESC, SortNumber ASC");
                int length = dt.Rows.Count;


                CheckBox[] cbCAT = new CheckBox[length];

                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    if (this.UpdatePanelCat.ContentTemplateContainer.Controls.Contains(this.UpdatePanelCat.ContentTemplateContainer.FindControl("CAT" + dt.Rows[i]["CATID"].ToString())))
                        continue;
                    cbCAT[i] = new CheckBox();
                    cbCAT[i].Text = dt.Rows[i]["CATCode"].ToString();
                    cbCAT[i].ID = "CAT" + dt.Rows[i]["CATID"].ToString();
                    cbCAT[i].Width = Unit.Parse("15%");
                    if (!Convert.ToBoolean(dt.Rows[i]["Active"].ToString()))
                        cbCAT[i].ForeColor = System.Drawing.Color.Red;
                    this.UpdatePanelCat.ContentTemplateContainer.Controls.Add(cbCAT[i]);
                }


                BEGDC.PreContract.BLL.ICATAssignment iCATAssignment = (BEGDC.PreContract.BLL.ICATAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CATAssignment_Service];
                dt = iCATAssignment.GetCATAssignmentActive(id.ToString());

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int a = 0; a < cbCAT.Length; a++)
                    {
                        if (cbCAT[a].ID == "CAT" + dt.Rows[i]["CATID"].ToString())
                            cbCAT[a].Checked = true;
                    }
                }

                cbCAT = null;
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void UpdateCAT()
        {
            try
            {
                string id = Request["id"].ToString();
                BEGDC.PreContract.BLL.ICATAssignment iCATAssignment = (BEGDC.PreContract.BLL.ICATAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CATAssignment_Service];
                BEGDC.PreContract.DAL.CATAssignment catAssignment = new BEGDC.PreContract.DAL.CATAssignment();
                BEGDC.PreContract.DAL.Employee employee = new BEGDC.PreContract.DAL.Employee();
                BEGDC.PreContract.DAL.CAT cat = new BEGDC.PreContract.DAL.CAT();
                CheckBox ck = new CheckBox();
                employee.EmployeeID = Convert.ToInt32(id);

                iCATAssignment.DeleteAllAssignment(id);

                if (this.cbCATAll.Checked)
                {
                    for (int i = 3; i < this.UpdatePanelCat.ContentTemplateContainer.Controls.Count; i++)
                    {
                        ck = (CheckBox)this.UpdatePanelCat.ContentTemplateContainer.Controls[i];

                        cat.CATID = Convert.ToInt32(ck.ID.Replace("CAT", String.Empty));
                        catAssignment.CATID = cat;
                        catAssignment.EmployeeID = employee;
                        catAssignment.Create();
                    }
                }

                else
                {
                    for (int i = 3; i < this.UpdatePanelCat.ContentTemplateContainer.Controls.Count; i++)
                    {
                        ck = (CheckBox)this.UpdatePanelCat.ContentTemplateContainer.Controls[i];
                        if (ck.Checked)
                        {
                            cat.CATID = Convert.ToInt32(ck.ID.Replace("CAT", String.Empty));
                            catAssignment.CATID = cat;
                            catAssignment.EmployeeID = employee;
                            catAssignment.Create();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void cbCATAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.cbCATAll.Checked)
                {
                    for (int i = 3; i < this.UpdatePanelCat.ContentTemplateContainer.Controls.Count; i++)
                    {
                        CheckBox ck = (CheckBox)this.UpdatePanelCat.ContentTemplateContainer.Controls[i];
                        ck.Checked = false;
                        ck.Enabled = false;
                    }
                }
                else
                {
                    for (int i = 3; i < this.UpdatePanelCat.ContentTemplateContainer.Controls.Count; i++)
                    {
                        CheckBox ck = (CheckBox)this.UpdatePanelCat.ContentTemplateContainer.Controls[i];
                        ck.Enabled = true;
                    }
                }
            }

            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }

        }
        #endregion

        #region Province
        protected void GetProvince()
        {
            try
            {
                int id = Convert.ToInt32(Request["id"].ToString());
                BEGDC.PreContract.BLL.IProvince iProvince = (BEGDC.PreContract.BLL.IProvince)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Province_Service];

                DataTable dt = iProvince.GetProvince_DataTable("1=1 ORDER BY Active DESC,SortNumber ASC");
                int length = dt.Rows.Count;

                CheckBox[] cbProvice = new CheckBox[length];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (this.UpdatePanelProvince.ContentTemplateContainer.Controls.Contains(this.UpdatePanelProvince.ContentTemplateContainer.FindControl("Province" + dt.Rows[i]["ProvinceID"].ToString())))
                        continue;
                    cbProvice[i] = new CheckBox();
                    cbProvice[i].ID = "Province" + dt.Rows[i]["ProvinceID"].ToString();
                    cbProvice[i].Text = dt.Rows[i]["ProvinceCName"].ToString();
                    cbProvice[i].Width = Unit.Parse("15%");
                    if (!Convert.ToBoolean(dt.Rows[i]["Active"].ToString()))
                        cbProvice[i].ForeColor = System.Drawing.Color.Red;
                    this.UpdatePanelProvince.ContentTemplateContainer.Controls.Add(cbProvice[i]);
                }


                BEGDC.PreContract.BLL.IProvinceAssignment iProvinceAssignment = (BEGDC.PreContract.BLL.IProvinceAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ProvinceAssignment_Service];
                dt = iProvinceAssignment.GetProvinceAssignmentActive(id.ToString());

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int a = 0; a < cbProvice.Length; a++)
                    {
                        if (cbProvice[a].ID == "Province" + dt.Rows[i]["ProvinceID"].ToString())
                            cbProvice[a].Checked = true;
                    }
                }

                cbProvice = null;
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void cbProvinceAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.cbProvinceAll.Checked)
                {
                    for (int i = 3; i < this.UpdatePanelProvince.ContentTemplateContainer.Controls.Count; i++)
                    {
                        CheckBox ck = (CheckBox)this.UpdatePanelProvince.ContentTemplateContainer.Controls[i];
                        ck.Enabled = false;
                        ck.Checked = false;
                    }
                }
                else
                {
                    for (int i = 3; i < this.UpdatePanelProvince.ContentTemplateContainer.Controls.Count; i++)
                    {
                        CheckBox ck = (CheckBox)this.UpdatePanelProvince.ContentTemplateContainer.Controls[i];
                        ck.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void UpdateProvince()
        {
            try
            {
                string id = Request["id"].ToString();
                BEGDC.PreContract.BLL.IProvinceAssignment iProvinceAssignment = (BEGDC.PreContract.BLL.IProvinceAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ProvinceAssignment_Service];
                BEGDC.PreContract.DAL.ProvinceAssignment provinceAssignment = new BEGDC.PreContract.DAL.ProvinceAssignment();
                BEGDC.PreContract.DAL.Employee employee = new BEGDC.PreContract.DAL.Employee();
                BEGDC.PreContract.DAL.Province province = new BEGDC.PreContract.DAL.Province();
                employee.EmployeeID = Convert.ToInt32(id);
                CheckBox ck = new CheckBox();

                iProvinceAssignment.DeleteAllAssignment(id);

                if (this.cbProvinceAll.Checked)
                {
                    for (int i = 3; i < this.UpdatePanelProvince.ContentTemplateContainer.Controls.Count; i++)
                    {
                        ck = (CheckBox)this.UpdatePanelProvince.ContentTemplateContainer.Controls[i];
                        province.ProvinceID = Convert.ToInt32(ck.ID.Replace("Province", String.Empty));
                        provinceAssignment.ProvinceID = province;
                        provinceAssignment.EmployeeID = employee;
                        provinceAssignment.Create();
                    }
                }
                else
                {
                    for (int i = 3; i < this.UpdatePanelProvince.ContentTemplateContainer.Controls.Count; i++)
                    {
                        ck = (CheckBox)this.UpdatePanelProvince.ContentTemplateContainer.Controls[i];
                        if (ck.Checked)
                        {
                            province.ProvinceID = Convert.ToInt32(ck.ID.Replace("Province", String.Empty));
                            provinceAssignment.ProvinceID = province;
                            provinceAssignment.EmployeeID = employee;
                            provinceAssignment.Create();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }

        }
        #endregion

        #region BD
        protected void GetBD()
        {
            try
            {
                int id = Convert.ToInt32(Request["id"].ToString());
                BEGDC.PreContract.BLL.IBD iBD = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];

                DataTable dt = iBD.GetBD_DataTable("1=1 ORDER BY Active DESC, SortNumber ASC");
                int length = dt.Rows.Count;

                CheckBox[] cbBD = new CheckBox[length];

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (this.UpdatePanelBD.ContentTemplateContainer.Controls.Contains(this.UpdatePanelBD.ContentTemplateContainer.FindControl("BD" + dt.Rows[i]["BDID"].ToString())))
                        continue;
                    cbBD[i] = new CheckBox();
                    cbBD[i].Text = dt.Rows[i]["BDCode"].ToString();
                    cbBD[i].ID = "BD" + dt.Rows[i]["BDID"].ToString();
                    cbBD[i].Width = Unit.Parse("15%");
                    if (!Convert.ToBoolean(dt.Rows[i]["Active"].ToString()))
                        cbBD[i].ForeColor = System.Drawing.Color.Red;
                    this.UpdatePanelBD.ContentTemplateContainer.Controls.Add(cbBD[i]);
                }


                BEGDC.PreContract.BLL.IBDAssignment iBDAssignment = (BEGDC.PreContract.BLL.IBDAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BDAssignment_Service];
                dt = iBDAssignment.GetBDAssignmentActive(id.ToString());

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int a = 0; a < cbBD.Length; a++)
                    {
                        if (cbBD[a].ID == "BD" + dt.Rows[i]["BDID"].ToString())
                            cbBD[a].Checked = true;
                    }
                }

                cbBD = null;
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void cbBDAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.cbBDAll.Checked)
                {
                    for (int i = 3; i < this.UpdatePanelBD.ContentTemplateContainer.Controls.Count; i++)
                    {
                        CheckBox ck = (CheckBox)this.UpdatePanelBD.ContentTemplateContainer.Controls[i];
                        ck.Enabled = false;
                        ck.Checked = false;
                    }
                }
                else
                {
                    for (int i = 3; i < this.UpdatePanelBD.ContentTemplateContainer.Controls.Count; i++)
                    {
                        CheckBox ck = (CheckBox)this.UpdatePanelBD.ContentTemplateContainer.Controls[i];
                        ck.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void UpdateBD()
        {
            try
            {
                string id = Request["id"].ToString();
                BEGDC.PreContract.BLL.IBDAssignment iBDAssignment = (BEGDC.PreContract.BLL.IBDAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BDAssignment_Service];
                BEGDC.PreContract.DAL.BDAssignment bdAssignment = new BEGDC.PreContract.DAL.BDAssignment();
                BEGDC.PreContract.DAL.Employee employee = new BEGDC.PreContract.DAL.Employee();
                BEGDC.PreContract.DAL.BD bd = new BEGDC.PreContract.DAL.BD();
                CheckBox ck = new CheckBox();
                employee.EmployeeID = Convert.ToInt32(id);

                iBDAssignment.DeleteAllAssignment(id);

                if (this.cbBDAll.Checked)
                {
                    for (int i = 3; i < this.UpdatePanelBD.ContentTemplateContainer.Controls.Count; i++)
                    {
                        ck = (CheckBox)this.UpdatePanelBD.ContentTemplateContainer.Controls[i];
                        bd.BDID = Convert.ToInt32(ck.ID.Replace("BD", String.Empty));
                        bdAssignment.BDID = bd;
                        bdAssignment.EmployeeID = employee;
                        bdAssignment.Create();
                    }

                }
                else
                {
                    for (int i = 3; i < this.UpdatePanelBD.ContentTemplateContainer.Controls.Count; i++)
                    {
                        ck = (CheckBox)this.UpdatePanelBD.ContentTemplateContainer.Controls[i];
                        if (ck.Checked)
                        {
                            bd.BDID = Convert.ToInt32(ck.ID.Replace("BD", String.Empty));
                            bdAssignment.BDID = bd;
                            bdAssignment.EmployeeID = employee;
                            bdAssignment.Create();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }
        #endregion

        #region Role
        protected void GetRole()
        {
            try
            {
                bool[] Role = this.CheckUserPermission();
                int id = Convert.ToInt32(Request["id"].ToString());
                BEGDC.PreContract.BLL.IRole iRole = (BEGDC.PreContract.BLL.IRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Role_Service];

                DataTable dt = iRole.GetRole_DataTable(String.Empty);

                this.cbRoleAdmin.Text = dt.Rows[0][2].ToString();
                this.cbRoleEditor.Text = dt.Rows[1][2].ToString();
                this.cbRoleViewer.Text = dt.Rows[2][2].ToString();
                this.cbRoleRoleAdmin.Text = dt.Rows[3][2].ToString();
                this.cbRoleCFI.Text = dt.Rows[4][2].ToString();
                this.cbRoleFeedbacker.Text = dt.Rows[5][2].ToString();
                this.cbRoleWeeklyViewer.Text = dt.Rows[6][2].ToString();
                this.cbRoleMonthlyViewer.Text = dt.Rows[7][2].ToString();

                BEGDC.PreContract.BLL.IUserRole iUserRole = (BEGDC.PreContract.BLL.IUserRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_UserRole_Service];
                dt = iUserRole.GetUserRole_DataTable(String.Format("EmployeeID = {0}", id));
                BEGDC.PreContract.DAL.Role role = new BEGDC.PreContract.DAL.Role();
                bool flag = true;

                for (int t = 0; t < dt.Rows.Count; t++)
                {
                    role = iRole.GetRole(Convert.ToInt32(dt.Rows[t]["RoleID"].ToString()));

                    if (role.Description.EndsWith(this.cbRoleRoleAdmin.Text))
                        this.cbRoleRoleAdmin.Checked = true;
                    else if (role.Description.EndsWith(this.cbRoleEditor.Text))
                        this.cbRoleEditor.Checked = true;
                    else if (role.Description.EndsWith(this.cbRoleViewer.Text))
                        this.cbRoleViewer.Checked = true;
                    else if (role.Description.EndsWith(this.cbRoleAdmin.Text))
                        this.cbRoleAdmin.Checked = true;
                    else if (role.Description.EndsWith(this.cbRoleCFI.Text))
                        this.cbRoleCFI.Checked = true;
                    else if (role.Description.EndsWith(this.cbRoleFeedbacker.Text))
                        this.cbRoleFeedbacker.Checked = true;
                    else if (role.Description.EndsWith(this.cbRoleWeeklyViewer.Text))
                        this.cbRoleWeeklyViewer.Checked = true;
                    else if (role.Description.EndsWith(this.cbRoleMonthlyViewer.Text))
                        this.cbRoleMonthlyViewer.Checked = true;
                }

                if (this.cbRoleAdmin.Checked)
                    flag = false;
                else if (this.cbRoleEditor.Checked || this.cbRoleViewer.Checked)
                    flag = true;
                else if (this.cbRoleRoleAdmin.Checked)
                    flag = false;

                this.rbtAll.Enabled = flag;
                this.rbtASB.Enabled = flag;
                this.rbtLucent.Enabled = flag;
                this.cbBDAll.Enabled = flag;
                this.cbCATAll.Enabled = flag;
                this.cbProvinceAll.Enabled = flag;
                this.DownloadChenckBox.Enabled = flag;
                this.CommentsCheckBox.Enabled = flag;

                for (int i = 3; i < this.UpdatePanelBD.ContentTemplateContainer.Controls.Count; i++)
                {
                    CheckBox ck = (CheckBox)this.UpdatePanelBD.ContentTemplateContainer.Controls[i];
                    ck.Enabled = flag;
                }

                for (int i = 3; i < this.UpdatePanelProvince.ContentTemplateContainer.Controls.Count; i++)
                {
                    CheckBox ck = (CheckBox)this.UpdatePanelProvince.ContentTemplateContainer.Controls[i];
                    ck.Enabled = flag;
                }

                for (int i = 3; i < this.UpdatePanelCat.ContentTemplateContainer.Controls.Count; i++)
                {
                    CheckBox ck = (CheckBox)this.UpdatePanelCat.ContentTemplateContainer.Controls[i];
                    ck.Enabled = flag;
                }

                if (!Role[1])
                {
                    this.cbRoleEditor.Enabled = false;
                    this.cbRoleRoleAdmin.Enabled = false;
                    this.cbRoleViewer.Enabled = false;
                    this.cbRoleAdmin.Enabled = false;
                    this.cbRoleCFI.Enabled = false;
                }
            }

            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void UpdateRole()
        {
            try
            {
                string id = Request["id"].ToString();
                BEGDC.PreContract.BLL.IUserRole iUserRole = (BEGDC.PreContract.BLL.IUserRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_UserRole_Service];
                BEGDC.PreContract.DAL.UserRole userRole = new BEGDC.PreContract.DAL.UserRole();
                BEGDC.PreContract.DAL.Role role = new BEGDC.PreContract.DAL.Role();
                BEGDC.PreContract.DAL.Employee emploee = new BEGDC.PreContract.DAL.Employee();

                emploee.EmployeeID = Convert.ToInt32(id);

                DataTable dt = iUserRole.GetUserRole_DataTable(String.Format("EmployeeID = {0}", emploee.EmployeeID));

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    userRole.UserRoleID = Convert.ToInt32(dt.Rows[i]["UserRoleID"].ToString());

                    userRole = iUserRole.GetUserRole(userRole.UserRoleID);
                    userRole.Delete();
                }

                userRole.EmployeeID = emploee;

                if (this.cbRoleAdmin.Checked)
                {
                    role.RoleID = 1;
                    userRole.RoleID = role;
                    userRole.Create();
                }

                if (this.cbRoleEditor.Checked)
                {
                    role.RoleID = 2;
                    userRole.RoleID = role;
                    userRole.Create();
                }

                if (this.cbRoleViewer.Checked)
                {
                    role.RoleID = 3;
                    userRole.RoleID = role;
                    userRole.Create();
                }

                if (this.cbRoleRoleAdmin.Checked)
                {
                    role.RoleID = 4;
                    userRole.RoleID = role;
                    userRole.Create();
                }

                if (this.cbRoleCFI.Checked)
                {
                    role.RoleID = 5;
                    userRole.RoleID = role;
                    userRole.Create();
                }

                if (this.cbRoleFeedbacker.Checked)
                {
                    role.RoleID = 6;
                    userRole.RoleID = role;
                    userRole.Create();
                }

                if (this.cbRoleWeeklyViewer.Checked)
                {
                    role.RoleID = 7;
                    userRole.RoleID = role;
                    userRole.Create();
                }

                if (this.cbRoleMonthlyViewer.Checked)
                {
                    role.RoleID = 8;
                    userRole.RoleID = role;
                    userRole.Create();
                }
            }

            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }
        #endregion


        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if ((this.cbRoleAdmin.Checked == false) && (this.cbRoleEditor.Checked == false)
                && (this.cbRoleViewer.Checked == false) && (this.cbRoleRoleAdmin.Checked == false)
                && (this.cbRoleCFI.Checked == false) && (this.cbRoleFeedbacker.Checked == false)
                && (this.cbRoleWeeklyViewer.Checked == false) && (this.cbRoleMonthlyViewer.Checked == false))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请至少选择一个角色!')</script>");
                return;
            }
            try
            {
                int userId = Convert.ToInt32(Request["id"].ToString());

                BEGDC.PreContract.BLL.IEmployee iEmployee = (BEGDC.PreContract.BLL.IEmployee)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Employee_Service];
                BEGDC.PreContract.DAL.Employee employee = iEmployee.GetEmployee(userId);

                BEGDC.PreContract.BLL.IUserRole iuserroleService = (BEGDC.PreContract.BLL.IUserRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_UserRole_Service];
                if (!this.cbRoleRoleAdmin.Checked) //不是角色管理员
                {
                    if (iuserroleService.IsAdmin(userId))   //原来是角色管理员. 现在不是了.判断是否是系统最后一个角色管理员
                    {
                        if (iuserroleService.IsUniqueAdmin(userId))
                        {
                            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + AlertMessage.UniqueAdmin.Replace("'", "\\'") + "')</script>");
                            this.cbRoleRoleAdmin.Checked = true;
                            return;
                        }
                    }

                }

                //this.UpdateRole();
                //if (this.cbRoleAdmin.Checked || this.cbRoleRoleAdmin.Checked)
                //{

                //    employee.Active = this.ActiveCheckBox.Checked;
                //    employee.Update();
                //}

                //if (this.cbRoleEditor.Checked || this.cbRoleViewer.Checked)
                //{
                this.UpdateRole();
                int asbOrLucent = 0;
                if (this.rbtASB.Checked)
                    asbOrLucent = 0;
                else if (this.rbtLucent.Checked)
                    asbOrLucent = 1;
                else if (this.rbtAll.Checked)
                    asbOrLucent = 2;

                employee.ASBOrLucent = asbOrLucent;
                employee.Active = this.ActiveCheckBox.Checked;
                employee.CommentAllowed = this.CommentsCheckBox.Checked;
                employee.DownloadAllowed = this.DownloadChenckBox.Checked;
                employee.Update();

                this.UpdateCAT();
                this.UpdateProvince();
                this.UpdateBD();
                //}

                BaseFunction.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.EditUser, string.Format("ADAccount: {0}", employee.ADAccount), User.Identity.Name);

                //else if(this.cbRoleRoleAdmin.Checked)
                //{
                //    this.UpdateRole();

                //    //employee.CommentAllowed = this.CommentsCheckBox.Checked;
                //    employee.Active = this.ActiveCheckBox.Checked;
                //    employee.Update();
                //}

                this.Response.Write("<script language='javascript'>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess) + "');var arr=new Array(); arr[0]=0; window.returnValue=arr;window.close();</script>");
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void cbRole_CheckedChanged(object sender, EventArgs e)
        {
            bool flag = false;

            if (this.cbRoleAdmin.Checked)
                flag = false;

            else if (this.cbRoleEditor.Checked || this.cbRoleViewer.Checked)
                flag = true;

            else if (this.cbRoleRoleAdmin.Checked)
                flag = false;

            for (int i = 3; i < this.UpdatePanelBD.ContentTemplateContainer.Controls.Count; i++)
            {
                CheckBox ck = (CheckBox)this.UpdatePanelBD.ContentTemplateContainer.Controls[i];
                ck.Enabled = flag;
            }

            for (int i = 3; i < this.UpdatePanelProvince.ContentTemplateContainer.Controls.Count; i++)
            {
                CheckBox ck = (CheckBox)this.UpdatePanelProvince.ContentTemplateContainer.Controls[i];
                ck.Enabled = flag;
            }

            for (int i = 3; i < this.UpdatePanelCat.ContentTemplateContainer.Controls.Count; i++)
            {
                CheckBox ck = (CheckBox)this.UpdatePanelCat.ContentTemplateContainer.Controls[i];
                ck.Enabled = flag;
            }

            this.cbBDAll.Enabled = flag;
            this.cbCATAll.Enabled = flag;
            this.cbProvinceAll.Enabled = flag;
            this.rbtASB.Enabled = flag;
            this.rbtLucent.Enabled = flag;
            this.rbtAll.Enabled = flag;
            this.DownloadChenckBox.Enabled = flag;
            this.CommentsCheckBox.Enabled = flag;
        }
    }
}
